package main

import (
	"fmt"
	"os"
	"time"

	logger "gitee.com/woodpile/wgs.ths.log.go"
)

func main() {
	SimpleLine()
}

func SimpleLine() {
	cfg := &logger.LoggerConfig{
		IgnoreStdout:           true,
		TimeFormat:             "%Y-%m-%d %H:%M:%S.%f",
		TimeFormatBufferLength: 27,
	}
	l, err := logger.NewLogger(cfg)
	if err != nil {
		fmt.Printf("Error exit: %v\n", err)
		os.Exit(1)
		return
	}
	{
		fn := "sample.log"
		_, err := os.ReadFile(fn)
		if err != os.ErrNotExist {
			os.Remove(fn)
		}
		logFile, err := logger.NewTargetFile(&logger.TargetFileConfig{
			Filename:            "sample",
			RotatePerTimeMinute: 3,
			RotatePerSizeKB:     20,
		})
		if err != nil {
			fmt.Printf("Error exit: %v\n", err)
			os.Exit(1)
			return
		}
		l.AddTarget(logFile)
	}

	ticker := time.NewTicker(time.Second)
	for range ticker.C {
		Logging(l)
	}
}

func Logging(l *logger.Logger) {
	l.DBG().Z()
	l.DBG().Msg("Hello, world!").Z()

	l.DBG().Pre("long").Msg("Helloooooooooo, world!").Z()
	l.DBG().Msg("Helloooooooooo, world!").Pre("long").Z()

	l.DBG().Pre("pre").S("key", "value").Msg("Hello, world!").Z()
	l.DBG().Pre("pre").I("int", 87).Msg("Hello, world!").Z()
	l.DBG().Pre("pre").U("uint", 87).Msg("Hello, world!").Z()
	l.DBG().Pre("pre").D("float", 87.4).Msg("Hello, world!").Z()
	l.DBG().Pre("pre").I("int", 87).U("uint", 87).D("float", 87.4).S("key", "value").Msg("Hello, world!").Z()
	l.DBG().
		Pre("pre").
		I("int", 87).U("uint", 87).D("float", 87.4).
		S("key", "value").
		Msg("Hello, world!").Z()

	l.DBG().Msg("Hello, world!").Z()
	l.DBG().Pre("pre").Msg("Hello, world!").Z()

	l.DBG().Msg("Hello, world!").Z()
	l.DBG().Msg("Hello, world!").Z()

	l.Level = logger.LevelTrace
	l.TRC().Msg("Hello, world!").Z()
	l.DBG().Msg("Hello, world!").Z()
	l.INF().Msg("Hello, world!").Z()
	l.WRN().Msg("Hello, world!").Z()
	l.ERR().Msg("Hello, world!").Z()
	l.FAT().Msg("Hello, world!").Z()

	l.Level = logger.LevelInfo
	l.TRC().Msg("Hello, world!").Z()
	l.DBG().Msg("Hello, world!").Z()
	l.INF().Msg("Hello, world!").Z()
	l.WRN().Msg("Hello, world!").Z()
	l.ERR().Msg("Hello, world!").Z()
	l.FAT().Msg("Hello, world!").Z()
	l.Level = logger.DefaultGlobalLevel

}
